package com.xiaouyudeguang.common.application;

import com.xiaouyudeguang.common.constants.Headers;
import com.xiaouyudeguang.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

import java.util.Arrays;

public class StartApplication extends SpringApplication {

    public StartApplication(Class<?>[] primarySources) {
        super(primarySources);
    }

    public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
        return new SpringApplication(primarySources).run(args);
    }

    public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
        System.setProperty("hibernate.dialect.storage_engine", "innodb");
        MDC.put(Headers.TRACE_ID, System.getProperty("USERNAME", "main"));
        Logger log = LoggerFactory.getLogger(primarySource);
        log.info("服务启动参数.{}", Arrays.asList(args));
        ConfigurableApplicationContext context = run(new Class<?>[]{primarySource}, args);
        ConfigurableEnvironment environment = context.getEnvironment();
        String appName = environment.getProperty("spring.application.name", "application");
        log.info("服务【{}】启动成功，端口是.{}", appName, StringUtils.getOrDefault(environment.getProperty("server.port"), "8080"));
        return context;
    }

}
